package com.ruoyi.framework.web.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ruoyi.wq.domain.WqAdUserLog;
import com.ruoyi.wq.domain.vo.WqAdUserLogVO;
import com.ruoyi.wq.service.IWqAdUserLogService;
import com.ruoyi.wx.domain.WxMaForm;
import com.ruoyi.wx.domain.WxMaUser;
import com.ruoyi.wx.service.WxMaUserService;

import cn.hutool.core.date.DateUtil;

@Component
public class AdUserLogService {
	
	@Autowired
	private WxMaUserService wxMaUserService;
	
	@Autowired
	private IWqAdUserLogService wqAdUserLogService;

	public boolean insertWqAdUserLog(WqAdUserLogVO wqAdUserLog){
		WxMaForm wxMaForm = new WxMaForm();
		wxMaForm.setAppId(wqAdUserLog.getAppId());
		wxMaForm.setCode(wqAdUserLog.getCode());
		WxMaUser wxMaUser = wxMaUserService.getUserInfo(wxMaForm);
		WqAdUserLog adLog = new WqAdUserLog();
		adLog.setAppId(wqAdUserLog.getAppId());
		adLog.setOpenId(wxMaUser.getOpenId());
		adLog.setAdAction(wqAdUserLog.getAdAction());
		adLog.setRemark(DateUtil.formatDate(new Date()));
		wqAdUserLogService.saveOrUpdate(adLog);
		return true;
	}

	public Integer getAdNumByCode(String appId, String code) {
		WxMaForm wxMaForm = new WxMaForm();
		wxMaForm.setAppId(appId);
		wxMaForm.setCode(code);
		WxMaUser wxMaUser = wxMaUserService.getUserInfo(wxMaForm);
		Integer num = wqAdUserLogService.getAdNumByOpenId(appId, wxMaUser.getOpenId());
		if(num == null){
			return 0;
		}
		return num;
	}
}
